<?php
namespace common\helps;
use Yii;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-12-02
 * Time: 15:33
 */
class qiniu{
    // 用于签名的公钥和私钥
    protected $accessKey = 'iC064XOK0D8rEYctRvhNV8LoWtkhhmopeeO2grBb';
    protected $secretKey = 'NONrIdKIRMXW-IG78X4Ttz9Vz7osUti0Z8_D2525';
    protected $bucket = 'kangyi';

    function uploadByQiNiu($filePath,$fileName){
        $token = $this->getUploadToken();
        $uploadMgr = new UploadManager();// 构建 UploadManager 对象
        list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath);    // 调用 UploadManager 的 putFile 方法进行文件的上传。
        if(!empty($err)){
            return false;
        }
        return $ret;
    }

    function getUploadToken(){
        // 初始化签权对象
        $auth = new Auth($this->accessKey,$this->secretKey);
        $uploadToken = $auth->uploadToken($this->bucket);// 生成上传Token
        return $uploadToken;

//        $redis = Yii::$app->cache;
//        $uploadToken = $redis->get('uploadToken');
//        if(!empty($uploadToken)){
//            return $uploadToken;
//        }else{
//            // 初始化签权对象
//            $auth = new Auth($this->accessKey,$this->secretKey);
//            $uploadToken = $auth->uploadToken($this->bucket);// 生成上传Token
//            $redis->set('uploadToken',$uploadToken,'30');
//            return $uploadToken;
//        }
    }





}